// Copyright  2004-2008 Backplane Ltd.
//
// This file is part of DOM3XPath
//
// DOM3XPath is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
//
// DOM3XPath is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License
// along with DOM3XPath. If not, see <http://www.gnu.org/licenses/>.
//	
//
//   $Id$
// XPathResult.h : Declaration of the CXPathResult

/*

	Issue XPath-25:
Should there be a reset method on the result in case someone wants to iterate the result multiple times?
It may be more trouble than it is worth, because the user can request a new query. See if there are use cases.


*/
//extern CComObject<IXMLDOMDocument2> * g_pXSL;
#ifndef __XPATHRESULT_H_
#define __XPATHRESULT_H_

#include "resource.h"       // main symbols
//#define COUNT_XPATHRESULT 1
/////////////////////////////////////////////////////////////////////////////
// CXPathResult
class ATL_NO_VTABLE CXPathResult : 
	public CComObjectRootEx<CComSingleThreadModel>,
	public CComCoClass<CXPathResult, &CLSID_XPathResult>,
	public ISupportErrorInfo,
	public IDispatchImpl<IXPathResult, &IID_IXPathResult, &LIBID_DOM3XPATHLib, 1, 1>
{
		friend class CXPathHelper;
public:
	#ifdef COUNT_XPATHRESULT
		static unsigned long sm_Count;	
	#endif


	CXPathResult():
		m_bBooleanValue(false)
		,m_bInvalidIteratorState(false)
	  	,m_dNumberValue(0)
		,m_sStringValue("")
		,m_ulSnapshotLength(0)
	{
	#ifdef COUNT_XPATHRESULT
		++sm_Count;	
	#endif


	}
	~CXPathResult()
	{
		ATLTRACE("\n");
		m_sStringValue.Empty();
	#ifdef COUNT_XPATHRESULT
		--sm_Count;	
	#endif

}

DECLARE_REGISTRY_RESOURCEID(IDR_XPATHRESULT)

DECLARE_PROTECT_FINAL_CONSTRUCT()

BEGIN_COM_MAP(CXPathResult)
	COM_INTERFACE_ENTRY(IXPathResult)
	COM_INTERFACE_ENTRY(IDispatch)
	COM_INTERFACE_ENTRY(ISupportErrorInfo)
END_COM_MAP()

// ISupportsErrorInfo
	STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);

// IXPathResult
public:
	STDMETHOD(snapshotItem)(unsigned long lIndex,IXMLDOMNode **retVal);
	STDMETHOD(iterateNext)(/*[out,retval]*/ IXMLDOMNode ** retVal);
	STDMETHOD(get_singleNodeValue)(/*[out, retval]*/ IXMLDOMNode * *pVal);
	STDMETHOD(get_snapshotLength)(/*[out, retval]*/ unsigned long *pVal);
	STDMETHOD(get_invalidIteratorState)(/*[out, retval]*/ BOOL *pVal);
	STDMETHOD(get_booleanValue)(/*[out, retval]*/ BOOL *pVal);
	STDMETHOD(get_stringValue)(/*[out, retval]*/ BSTR *pVal);
	STDMETHOD(get_numberValue)(/*[out, retval]*/ double *pVal);
	STDMETHOD(get_resultType)(/*[out, retval]*/ short *pVal);

	enum EXPathResultType {
		ANY_TYPE = 0,
		NUMBER_TYPE = 1,
		STRING_TYPE = 2,
		BOOLEAN_TYPE = 3,
		UNORDERED_NODE_ITERATOR_TYPE = 4,
		ORDERED_NODE_ITERATOR_TYPE = 5,
		UNORDERED_NODE_SNAPSHOT_TYPE = 6,
		ORDERED_NODE_SNAPSHOT_TYPE = 7,
		ANY_UNORDERED_NODE_TYPE = 8,
		FIRST_ORDERED_NODE_TYPE = 9
	};
	HRESULT parseSelection(IXMLDOMNodeList * pDOMSelection);

	EXPathResultType m_uResultType;

private:
	// This should be a union!
	double m_dNumberValue;
	CComBSTR m_sStringValue;
	BOOL m_bBooleanValue;
	CComQIPtr<IXMLDOMNode> m_pSingleNodeValue;
	bool m_bInvalidIteratorState;
	unsigned long m_ulSnapshotLength;
	CComQIPtr<IXMLDOMNodeList> m_pNodeList;
};

#endif //__XPATHRESULT_H_
